#ifndef MYHUTU_H
#define MYHUTU_H

#include <QFrame>
#include <QPainter>
#include <QPaintEvent>
#include <QWheelEvent>
#include <QVector>
#include <QColor>
#include <QToolBar>
#include <QImage>
#include <QStack>
#include <QAction>
#include <QFont>
#include <QTextEdit>
#include <QApplication>


struct LineAndColor
{
LineAndColor(const QVector<QLine>& vLine, const QColor& color,
            int widths, const QVector<QRect>& vRect,
            bool isEllips = false, bool isRect = false)
        : vLine(vLine), color(color), widths(widths),
          vRect(vRect), isEllips(isEllips), isRect(isRect) {}

    QVector<QLine> vLine;
    QVector<QRect> vRect;
    QColor color;
    int widths;
    bool isEllips;// 标识是否为椭圆
    bool isRect; // 标识是否为矩形
};
struct TextAndPosition {
TextAndPosition(const QString &text, const QPoint &position, const QColor &color)
        : text(text), position(position), color(color) {}
    QString text;//输入的文本
    QPoint position;//位置
    QColor color; // 添加颜色成员变量
    QFont font;
    bool isTextEditing;

};
enum ActionType {
    DRAW_LINE,
    DRAW_RECT,
    DRAW_ELLIPS,
    ADD_TEXT
};
struct Action {
    ActionType type;
    QVariant data;
    QColor color;
    int width;

    Action(ActionType type, const QVariant& data, QColor color, int width)
        : type(type), data(data), color(color), width(width) {}
};



class myhutu : public QFrame
{
    Q_OBJECT
public:
    explicit myhutu(QWidget *parent = nullptr);
    void timerEvent(QTimerEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void paintEvent(QPaintEvent *event) override;
    void wheelEvent(QWheelEvent *event);
    void SavePic();
    void OpenPic();
    void AddTexts();
    void Texts();
    void SaveAsPic();
    void SaveImage(const QString &fileName);
    void addAction(ActionType type, const QVariant& data, QColor color, int width);
    void saveDrawingToImage(QImage& image);

     bool handleTextEditing();
    void startTextDragging(QMouseEvent *event);
    void updateTextPosition(QMouseEvent *event);
    bool isInTextEditMode();
    virtual bool eventFilter(QObject *watched, QEvent *event) override;
    virtual void keyPressEvent(QKeyEvent *event) override;
    void resetTextStates();
    void undo();
    virtual ~myhutu();  // 析构函数声明
signals:

public slots:


public:
    bool mousePress;
    QColor color;
    int linesize;
    double scaleLevel;
    QImage loadedImage;
    bool isDragging;
    bool isDrawingLine;
    bool isDrawingCurve;
    bool isDrawingEllips;
    bool isDrawingRect;
    bool isAddText;//判断是否要编辑文字
    QPoint textPosition;//文本位置
    QColor textColor; // 文字颜色
     bool isTextEditing;
    bool isTextDragging;  // 标记文字是否正在被拖动
    TextAndPosition *currentDraggingText;  // 指向当前正在被拖动的文字信息
    bool isScaling;
    QPointF offset;
    QPen pen;
    QPoint startpos;
    QPoint endpos;
    QPoint currentpos;
    QVector<QLine> V;
    QVector<LineAndColor> vLineAndColor;//存储所有线条和颜色
    QVector<TextAndPosition> vTextAndPosition; // 保存所有文字和位置的列表
    // QVector<QLine> tempLines;
    QLine linet;
    QRect ellipsRect;
    QPainter painter;
    QImage scaledImage;
    QStack<QVector<Action>> history; // 历史记录栈
    bool canUndo; // 是否可以撤销
    QVector<Action> vActions;
    QTextEdit *textEdit;
    bool isTextDrawingFinished;
    QMouseEvent* lastMouseEvent;
    bool isTextBeingEdited;  // 用于标识是否正在有效编辑文字

};

#endif // MYHUTU_H
